<?php
/* Joseph April 2013
* Send Email with Attachment.
* Use of PHPMailer library - https://github.com/Synchro/PHPMailer
* Do not remove any lines below.
* This is as part of HeartEsteem project
*/
require_once('phpmailer/class.phpmailer.php');  //PHPMailer class

if(!empty($_POST['sender_name']) && !empty($_POST['email_id']) && !empty($_REQUEST['data']))
{
    try{
        //save the drawing image first
        require_once("save_image.php");

        //now set a cookie with the image path name
        if(!isset($_COOKIE['test'])){ //if cookie not present
            //setting a cookie that will leave for a browser session only. $uniqueId is defined inside save_image.php
            setcookie('DYHmkttzx',$prefix . $uniqueId); //64 bit hash + 32 bit unique id
        }

        //now send email
        $mail = new PHPMailer(); // defaults to using php "mail()" , so no SMTP used here

        //$body = file_get_contents('email_test/phpmailer_demo/contents.html');   //get the HTML contents for the mail

        $mail->SetFrom('noreply@drawyourheart.com',$_POST['sender_name']);  //set sender's details - From part $_POST['sender_name']

        $address = $_POST['email_id']; //email address to send to
        $mail->AddAddress($address);  //set reciever's address - To part

        $mail->Subject    = "Check out my drawing HeartDraw!!";

        $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // Alt Body

        $body = "<html><body style='font-family:Trebuchet MS,Tahoma,Verdana,Arial,sans-serif;'><b>" . $_POST['email_desc'] . "</b>";
        $body .= file_get_contents('phpmailer/contents.html');
        $body .= "</body></html>";
        $mail->MsgHTML($body);

        $mail->AddAttachment($target_path);      // attachment

        if(!$mail->Send()) {
          echo $mail->ErrorInfo;
          //echo "Something went wrong. If this problem persists then contact the admin";
        } else {
          echo "Email sent to '" . $_POST['email_id'] . "'";
        }
    }
    catch(Exception $e){
        echo "Something went wrong. If this problem persists then contact the admin"; //. $e->getMessage();
    }
}
else{
    echo "Empty fields not allowed!";
}
?>